package com.inspur.security.cbb3.kms.config.security;

import com.inspur.security.cbb3.kms.utils.ServletUtil;
import com.inspur.security.cbb3.kms.enums.SystemCode;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * 认证失败处理类
 * @author lijunchang
 */
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {

    private static final long serialVersionUID = -8970718410437077606L;

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        //验证为未登陆状态会进入此方法，认证错误
        ServletUtil.returnException(response, SystemCode.ERR_USER_INFO_INVALID, HttpStatus.UNAUTHORIZED);
    }
}
